<?php
set_time_limit(0);
elgg_load_css('style.css');

/**
 * Get the user interface for the groups that were detected by clustering.
 */
function get_recommender_group() 
{
	$relationship = 'group_element'; 
	$ut = elgg_get_logged_in_user_entity();
	$user_groups = elgg_get_entities(array('type' => 'group','limit'=>0,'owner_guid' => $ut->getGUID() ) );
	
	
	if(count($user_groups) > 0)
	{
		
		
		$dic_group_c_n = array(); //Array where even positions are guids of clusters and odd positions are the number of groups in that clusters
		echo "<p class='title'>Groups:</p>";
		for($i=0;$i<count($user_groups);$i++)
		{
				$guid_rel_group = elgg_get_entities_from_relationship(array('relationship' => $relationship,'relationship_guid'=> $user_groups[$i]->getGUID(),'inverse_relationship'=> TRUE ) );
				
				if(count($guid_rel_group) > 0)
				{
				
				 
				 
				if(count($dic_group_c_n) == 0)
				{
					
					
					$dic_group_c_n[0] = $guid_rel_group[0]->getGUID();
					$dic_group_c_n[1] = 1;   	
				}
				else
				{	
					
					
					
					$flag = false; 
					for($j=0;$j< count($dic_group_c_n);$j++)
					{
						if($j%2 == 0)
						{
							
							//odd
							if($guid_rel_group[0]->getGUID() == $dic_group_c_n[$j])
							{
								
								$flag = true; 
								$dic_group_c_n[$j+1] += 1; 
								break;
							}
						}
					}
					
					
					
					if(!$flag)
					{
						$dim = count($dic_group_c_n);
						
						$dic_group_c_n[$dim] = $guid_rel_group[0]->getGUID(); 
						$dic_group_c_n[$dim +1] = 1; 
					}
					
				}
			}
			
				
		}
		
		$max = 0; 
		// get the cluster with highest number
		for($i=0;$i<count($dic_group_c_n);$i++)
		{
			if(($i%2 == 0) && ($dic_group_c_n[$i+1] > $dic_group_c_n[$max+1]))
			{	
				
				$max = $i; 
			}
		}
		
		
		
		$find_group = elgg_get_entities_from_relationship(array('relationship' => $relationship,'relationship_guid'=> $dic_group_c_n[$max]));
		
		for($i=0;$i<count($find_group);$i++)
		{
			if($i < 5)
			{
				echo elgg_view_entity($find_group[$i]);	
				
			}
		}
		echo "<hr width=50% align='center'/> ";
		 
	}
}
/**
 * Get the user interface for the users that were detected by clustering.
 */
function get_recommender_user()
{
	$ut = elgg_get_logged_in_user_entity();
	$guid_rel_user = elgg_get_entities_from_relationship(array('relationship' => 'user_element','relationship_guid'=> $ut->getGUID(),'inverse_relationship'=> TRUE ) );
	if(count($guid_rel_user) > 0)
	{
		$guid_user = elgg_get_entities_from_relationship(array('relationship' => 'user_element','relationship_guid'=> $guid_rel_user[0]->getGUID() ) );
		
		
		
			if($guid_user > 0)
			{
				
				echo "<p class='title'>Friend:</p>"; 
				for($i=0;$i< count($guid_user);$i++)
				{
					if(($guid_user[$i]->getGUID() != $ut->getGUID()) && (!$ut->isFriendsWith($guid_user[$i]->getGUID() ) ) && $i < 5 	)
					{
						echo    elgg_view_entity($guid_user[$i]);	
					}
				} 
				
				echo "<hr width=50% align='center'/> ";
			}
			else
			{
				echo "<p>No suggestions yet.</p>"; 
			}
	}
	else
	{
		echo "<p>No suggestions yet.</p>"; 
	}

}

/**
 * Get the user interface for the objects that were detected by clustering.
 * @param $entity_name Name of subtype of object.
 */   
function get_recommender_object($entity_name)
{
	$relationship = $entity_name . '_element'; 
	$ut = elgg_get_logged_in_user_entity();
	$user_objects = elgg_get_entities(array('type' => 'object','subtype' => $entity_name,'limit'=>0,'owner_guid' => $ut->getGUID() ) );
	
	if(count($user_objects) == 0)
	{
		echo "<p class='title'>". $entity_name .":</p>";
		echo "<p>No ". $entity_name ." created by the user</p>";
	}
	else
	{
		
		echo "<p class='title'>". $entity_name .":</p>";
		$dic_object_c_n = array(); //Array where even positions are guids of clusters and odd positions are the number of objects in that clusters
		for($i=0;$i<count($user_objects);$i++)
		{
				$guid_rel_object = elgg_get_entities_from_relationship(array('relationship' => $relationship,'relationship_guid'=> $user_objects[$i]->getGUID(),'inverse_relationship'=> TRUE ) );
				
				if(count($guid_rel_object)>0)
				{
				 
				 
						if(count($dic_object_c_n) == 0)
						{
							
							
							$dic_object_c_n[0] = $guid_rel_object[0]->getGUID();
							$dic_object_c_n[1] = 1;   	
						}
						else
						{	
							
							
							
							$flag = false; 
							for($j=0;$j< count($dic_object_c_n);$j++)
							{
								if($j%2 == 0)
								{
									
									//odd
									if($guid_rel_object[0]->getGUID() == $dic_object_c_n[$j])
									{
									
										$flag = true; 
										$dic_object_c_n[$j+1] += 1; 
										break;
									}
								}
							}
							
							
							
							if(!$flag)
							{
								$dim = count($dic_object_c_n);
								
								$dic_object_c_n[$dim] = $guid_rel_object[0]->getGUID(); 
								$dic_object_c_n[$dim +1] = 1; 
							}
							
						}//end else
			}
						
		}
		
		$max = 0; 
		// get the cluster with highest number
		for($i=0;$i<count($dic_object_c_n);$i++)
		{
			if(($i%2 == 0) && ($dic_object_c_n[$i+1] > $dic_object_c_n[$max+1]))
			{	
				
				$max = $i; 
			}
		}
		
		
		$find_object = elgg_get_entities_from_relationship(array('relationship' => $relationship,'relationship_guid'=> $dic_object_c_n[$max]));
		
		for($i=0;$i<count($find_object);$i++)
		{
			if($i < 5)
			{
				echo elgg_view_entity($find_object[$i]);	
			}
		}
	
		echo "<hr width=50% align='center'/> "; 
	}
}

/**
 * Represents this plugin.
 */
$plugin = elgg_get_plugin_from_id('mtcd');

/**
 * List of settings for this plugin.
 */
$settings = $plugin->getAllSettings();

	if(	isset($settings['recommender_user'] ) && $settings['recommender_user'] == 'true'  )
	{
			
		get_recommender_user();
		echo "<br />";
	}
	if(	isset($settings['recommender_group'] ) && $settings['recommender_group'] == 'true'  )
	{
		
		get_recommender_group();
		echo "<br />";
	}
	if(	isset($settings['recommender_blog'] ) && $settings['recommender_blog'] == 'true'  )
	{
		
		get_recommender_object('blog');
		echo "<br />";
	}
	if(	isset($settings['recommender_file'] ) && $settings['recommender_file'] == 'true'  )
	{
		
		get_recommender_object('file');
		echo "<br />";
	}
	if(	isset($settings['recommender_bookmarks'] ) && $settings['recommender_bookmarks'] == 'true'  )
	{
		
		get_recommender_object('bookmarks');
		echo "<br />";
	}
	if(	isset($settings['recommender_page'] ) && $settings['recommender_page'] == 'true'  )
	{
		
		get_recommender_object('page');
	}
?>
